﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MINERP.MODEL.FoundationModel;
using MINERP.DAL;
using MINERP.MODEL.PaperManager;
using MINERP.COM;

namespace MINERP.BLL.Common
{
    /// <summary>
    /// 相纸的管理逻辑类
    /// </summary>
    public class PaperInfo_BLL
    {
        /// <summary>
        /// 得到所有没有完成的相纸信息
        /// </summary>
        /// <returns></returns>
        public static List<PaperInfo_info> GetAllNotOverPaper()
        {
            List<PaperInfo_info> resultList = new List<PaperInfo_info>();
            int paperOverStatus = (int)MINERP.COM.PaperInfoStatus.Complete;
            using ( newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                foreach (var p in db.PaperInfo.Where(p => p.Status != paperOverStatus).ToList())
                {
                    if (!p.paperFaceReference.IsLoaded)
                    {
                        p.paperFaceReference.Load();
                    }
                    if (!p.paperTypeReference.IsLoaded)
                    {
                        p.paperTypeReference.Load();
                    }
                    resultList.Add(new PaperInfo_info()
                    {
                        AddTime = (DateTime)p.AddTime,
                        Length = (double)p.Length,//米
                        PaperFaceName = p.paperFace.Name,
                        PaperFaceID = p.paperFace.PaperFaceID,
                        PaperTypeID = p.paperType.PaperTypeID,
                        PaperTypeName = p.paperType.Name,
                        Width = (double)(p.Width*100)/2.54,
                        Status = (int)p.Status,
                        PrintPaperId = p.PrintPaperId
                    });
                }
            }

            return resultList;
        }

        /// <summary>
        /// 添加新的纸
        /// </summary>
        /// <param name="newList"></param>
        /// <param name="newPaperCodeList">新新的ID，用于打印条码</param>
        /// <returns></returns>
        public static int AddNewPaperInfo(List<PaperInfo_info> newList, out List<int> newPaperCodeList)
        {
            int result = 0;
            newPaperCodeList = new List<int>();
            List<PaperInfo> listT = new List<PaperInfo>();
            if (newList.Count > 0)
            {
                using (newSampleYPDBEntities  db = new  newSampleYPDBEntities())
                {

                    foreach (var pi in newList)
                    {
                        PaperInfo piNew = new PaperInfo()
                        {
                            AddTime = UnityMethod.GetSystemTime(),
                            EndPaperLength = 0,
                            PaperBreakArea = 0,
                            LastBordId = 0,
                            LastLength = pi.Length,
                            Length = pi.Length,
                            LastPrintTime = DateTime.Now,
                            PaperEndArea = 0,
                            //PrinterID = 0,
                            Remark = pi.Remark,
                            Status = (int)MINERP.COM.PaperInfoStatus.New,
                            Width = (pi.Width*2.54)/100,
                            paperFace = db.paperFace.FirstOrDefault(pf => pf.PaperFaceID == pi.PaperFaceID),
                            paperType = db.paperType.FirstOrDefault(pt => pt.PaperTypeID == pi.PaperTypeID),
                            PaperTitleArea = 0,
                            PaperYieldArea = 0
                        };
                        db.AddToPaperInfo(piNew);
                        listT.Add(piNew);
                    }

                    try
                    {
                        db.SaveChanges();
                        newPaperCodeList = listT.Select(p => p.PrintPaperId).ToList();
                    }
                    catch (Exception)
                    {
                        result = -2;
                    }
                }
            }
            else
            {
                result = -1;
            }
            return result;
        }

        public static int ModifyPaperInfo(PaperInfo_info modfifyPI)
        {
            int result = 0;
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                PaperInfo pi = db.PaperInfo.FirstOrDefault(p => p.PrintPaperId == modfifyPI.PrintPaperId);
                if (pi != null)
                {
                    pi.paperFace = db.paperFace.FirstOrDefault(pf => pf.PaperFaceID == modfifyPI.PaperFaceID);
                    pi.paperType = db.paperType.FirstOrDefault(pt => pt.PaperTypeID == modfifyPI.PaperTypeID);
                    pi.Length = modfifyPI.Length;
                    pi.Width = (modfifyPI.Width*2.54)/100;
                }
                else
                {
                    result = -1;
                }
                try
                {
                    db.SaveChanges();
                }
                catch (Exception)
                {

                    result = -2;
                }
            }
            return result;
        }

        public static void DelPaper(PaperInfo_info _SelectedPaperInfo)
        {
            if (_SelectedPaperInfo != null)
            {
                using (newSampleYPDBEntities db = new newSampleYPDBEntities())
                {
                    PaperInfo pi = db.PaperInfo.FirstOrDefault(p => p.PrintPaperId == _SelectedPaperInfo.PrintPaperId);
                    if (pi != null)
                    {
                        db.DeleteObject(pi);
                    }
                    try
                    {
                        db.SaveChanges();
                    }
                    catch
                    {

                    }
                }
            }
        }

        /// <summary>
        /// 查询某一范围的纸
        /// </summary>
        /// <param name="PrinterIds">打印机ID</param>
        /// <param name="PTId">纸面 ID</param>
        /// <param name="PFId">纸型ID</param>
        /// <param name="startTime">开始时间</param>
        /// <param name="endTime">结束时间</param>
        /// <returns></returns>
        public static List<PaperInfo_Info> GetSomePaperUseStat(List<int> PrinterIds, List<int> PTIds, List<int> PFIds, DateTime startTime, DateTime endTime)
        {
            List<PaperInfo_Info> resultList = new List<PaperInfo_Info>();
           
            int paperNewState=(int)MINERP.COM.PaperInfoStatus.New;
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                foreach (var pi in db.PaperInfo.Include("PaperFace").Include("PaperType").Include("printer").Where(p => p.Status != paperNewState && p.LastPrintTime >= startTime && p.LastPrintTime <= endTime).ToList().Where(p => PrinterIds.Contains(p.printer.PrinterID) && PFIds.Contains(p.paperFace.PaperFaceID) && PTIds.Contains(p.paperType.PaperTypeID)).ToList())
                {
                    if (!pi.paperTypeReference.IsLoaded)
                    {
                        pi.paperTypeReference.Load();
                    }
                    if (!pi.paperFaceReference.IsLoaded)
                    {
                        pi.paperFaceReference.Load();
                    }
                    if (!pi.PaperWasteRecords.IsLoaded)
                    {
                        pi.PaperWasteRecords.Load();
                    }

                    PaperInfo_Info pT = new PaperInfo_Info()
                    {
                        AddTime = (DateTime)pi.AddTime,
                        LastPrintTime = (DateTime)pi.LastPrintTime,
                        Length = (double)pi.Length,
                        PaperFaceName = pi.paperFace.Name,
                        PaperTypeName = pi.paperType.Name,
                        PrintPaperId = pi.PrintPaperId,
                        Remark=pi.Remark,                        
                        PrinterID = pi.printer.PrinterID,
                        Status = (int)pi.Status,
                        Width = (double)pi.Width,
                        UseRecords=new List<PaperRecords_Info> (),
                        PrintBoardArea=pi.PaperYieldArea,
                        BarCodeAndBBArea=pi.PaperBreakArea,
                        LastLength=pi.LastLength??0,
                        PaperConsumptionLength=pi.PaperConsumptionLength                        
                    };       
             
                    foreach (var r in pi.PaperWasteRecords.ToList())
                    {
                        if(r.PrinterWaste)
                        {
                            if (!r.PaperWasteTypeReference.IsLoaded)
                            {
                                r.PaperWasteTypeReference.Load();
                            }
                            
                            pT.UseRecords.Add(new PaperRecords_Info()
                            {
                                AddTime = r.CreateTime,
                                Length = Math.Round(r.Length,4),
                                PaperId = pi.PrintPaperId,
                                RecordId = r.Id,
                                RecordName = r.PaperWasteType.PaperOperationTypeName,
                                RecordTypeId = r.PaperWasteType.Id,
                                Status = r.Status,
                                Width = Math.Round(r.Width,4)
                            });
                            //pT.WateLength += r.Length;
                            pT.WrasteArea += Math.Round(r.Length * r.Width, 4);
                        }
                    }
                    if (pT.Status==(int)PaperInfoStatus.Complete&&pT.LastLength<0)
                    {
                        //pT.WateLength -= (pT.PaperConsumptionLength - pT.Length);
                        //pT.WateLength = pT.WateLength < 0 ? 0 : pT.WateLength;
                        pT.WrasteArea -= Math.Round((pT.PaperConsumptionLength - pT.Length) * pT.Width, 4);
                        pT.WrasteArea = pT.WrasteArea < 0 ? 0 : pT.WrasteArea;
                    }
                   
                    resultList.Add(pT);
                }
            }

             return resultList;
        }
    }
}
